﻿using Library.Common.Model;
using Library.Common.Serialize;
using Library.Core.Application.BaseAction;
using Library.Core.Application.IInfrastructure;
using Library.Core.Domain.Repository;
using Synthesize.BaseSet.Domain.Aggregation.Dtos.Authority.Post;
using Synthesize.BaseSet.Domain.Aggregation.Entity.Authority;
using Synthesize.BaseSet.Domain.Repository.Authority;
using Synthesize.BaseSet.Domain.Specifications.Authority.Post;
using Synthesize.BaseSet.ServiceInterface.Dtos.Authority.Post;
using Synthesize.BaseSet.ServiceInterface.Query.Authority;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Synthesize.BaseSet.Application.Query.Authority
{
    /// <summary>
    /// 岗位查询逻辑
    /// </summary>
    public class PostQueryService : BaseService, IPostQueryService
    {
        private readonly IPostRepository _postRepository;

        public PostQueryService(IIocContainer container, IPostRepository postRepository) : base(container)
        {
            _postRepository = postRepository;
        }

        /// <summary>
        /// 获取岗位分页列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<BaseApiResult<PageData<GetPostListOutput>>> GetPostPageList(GetPostListInput input)
        {
            return await HandleAsync(input, async () =>
            {
                var result = new PageData<GetPostListOutput>();

                var orderExp = input.OrderParms.ConvertToOrderExpression<GetPostListOutDto>();
                var spceifications = new GetPostListSpceification(input.Keywords).SatisfiedBy();
                var list = await _postRepository.GetPostPageList(input.Page, input.PageSize, spceifications, orderExp, input.ReadOnly);

                if (list.Rows.Any())
                {
                    result.Rows = MapperHelper<GetPostListOutDto, GetPostListOutput>.MapList(list.Rows);
                    result.Page = input.Page;
                    result.PageSize = input.PageSize;
                    result.Total = list.Total;
                }

                return result;
            });
        }


        /// <summary>
        /// 获取所有岗位列表
        /// </summary>
        /// <returns></returns>
        public async Task<BaseApiResult<List<GetPostListOutput>>> GetAllList() 
        {
            return await HandleAsync(async () =>
            {
                var result = new List<GetPostListOutput>();
                var list = await _postRepository.GetManyAsync(x => true);
                if (list.Any())
                {
                    result = MapperHelper<PostInfoEntity, GetPostListOutput>.MapList(list);
                }
                return result;
            });
        }
    }
}
